package com.okq.util.filter;

import org.apache.commons.lang.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 描述 : 验证码过滤器
 * 作者 : zdl
 * 日期 : 2017/3/15 13:23
 */
public class CheckCodeFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest hsr = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		String checkCode = hsr.getParameter("checkCode");
		String sessionRand = (String) hsr.getSession().getAttribute("rand");

		if (!StringUtils.isEmpty(checkCode) && !StringUtils.isEmpty(sessionRand)) {
			if (checkCode.equals(sessionRand)) {
				chain.doFilter(req, res);
			} else {
				response.sendRedirect("login.jsp?loginFailure=codeError");
			}
		} else {
			response.sendRedirect("login.jsp?loginFailure=codeError");
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}
